--- interact_link: content/08/09/plotly8-9.ipynb kernel_name: python3 kernel_path: content/08/09 has_widgets: false title: |- Facet Plots pagenum: 22 prev_page: url: /08/08/plotly8-8.html next_page: url: /08/10/plotly8-10.html suffix: .ipynb search: facet id plot plots want compare plotlydistributionfacetplot ly python trellis factet good different views same data multiple categorical variables plotlydistributionfacetplotbasic basic plotlydistributionfacetplothistogram histogram plotlydistributionfacetplotbox box comment: "***PROGRAMMATICALLY GENERATED, DO NOT EDIT. SEE ORIGINAL FILES IN /content***" ---
Facet Plots

Facet Plots

Factet plots are good to use:

  • When you want to compare different views of same data.
  • When you want to compare multiple categorical variables.

Basic Facet Plot

import plotly.figure_factory as ff
import plotly.graph_objects as go
import pandas as pd
from plotly.offline import init_notebook_mode, plot
from IPython.core.display import display, HTML
init_notebook_mode(connected=True)

mtcars = pd.read_table('https://raw.githubusercontent.com/plotly/datasets/master/mpg_2017.txt',sep='\t')


fig = ff.create_facet_grid(
    mtcars,
    x='hwy',
    y='cty',
    facet_row='class',
    color_name='class',
    color_is_cat=True,
)

plot(fig, filename = 'figure8-9-1.html')
display(HTML('figure8-9-1.html'))

Histogram Facet Plot

import pandas as pd
tips = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/tips.csv')

fig = ff.create_facet_grid(
    tips,
    x='total_bill',
    y='tip',
    facet_row='sex',
    facet_col='smoker',
    trace_type='histogram',
)

plot(fig, filename = 'figure8-9-2.html')
display(HTML('figure8-9-2.html'))

Box Facet Plot

fig = ff.create_facet_grid(
    tips,
    y='tip',
    facet_row='sex',
    facet_col='smoker',
    trace_type='box',
    facet_col_labels='Smoker',
    facet_row_labels='Sex'
)

plot(fig, filename = 'figure8-9-3.html')
display(HTML('figure8-9-3.html'))